define(
	[
	appBasePath + '/scripts/module.js',
	],
	function (substanceModule) {

    'use strict';

    substanceModule.controller('Substance.StoreOutController', [
        '$scope',
        '$state',
        '$stateParams',
        'gbits.websns.substance.config',
        'Substance.GoodsService',
        'Substance.GoodsApplyService',
        '$timeout',
        function($scope, $state, $stateParams, config, goodsService, goodsApplyService, $timeout) {
        	
        	// 默认待审批
        	$scope.type = 3;
        	$scope.page = {
        			currentPage: 1
        	};
        	
        	$scope.queryData = {name: ''};
        	
        	var ld = function loadData() {
        		$scope.list = [];
        		goodsApplyService.list($scope.type, {currentPage: $scope.page.currentPage, name: $scope.queryData.name}).then(function(data) {
        			$timeout(function(){
        				$scope.list = data.data;
            			$scope.page = data;
            			$scope.page.prePage = 1;
            			$scope.page.nextPage = data.lastPage;
            			if (data.currentPage > 1) {
            				$scope.page.prePage = data.currentPage - 1;
            			}
            			if (data.currentPage < $scope.page.lastPage - 1) {
            				$scope.page.nextPage = data.currentPage + 1;
            			}
            			$scope.page.showPages = [];
            			for (var i = data.currentPage - 5; i<=data.lastPage; i++) {
            				if (i>0) {
            					$scope.page.showPages.push(i);
            				}
            				if ($scope.page.showPages.length > 10) {
            					break;
            				}
            			}
        			})
        			
        		});
        	}
        		
        	$scope.setType = function(type) {
        		$timeout(function(){
        			$scope.type = type;
        			$scope.page = {
                			currentPage: 1
                	};
        			$scope.loadData();
        		});
        	};
        	
        	$scope.confirmStoreout = function(item){
        		$timeout(function(){
        			$(".background-panel").css("display","block");
        			$("#goods-storeout").attr("gid", item.id).css("display","block");
        			$("#goods-storeout .cago-close").bind('click', function(){
        				$(".background-panel").css("display","none");
        				$("#goods-storeout").css("display","none");
        			});
        		});
        		
        	};
        	$scope.storeout = function(item){
        		$timeout(function(){
        			var id = $("#goods-storeout").attr('gid');
            		goodsApplyService.storeout(id, {}).then(function(data){
            			if (data.success) {
            				$(".background-panel").hide();
            				$("#goods-storeout").hide();
            				$scope.loadData();
            			} else {
            				$scope.errorMsg(data);
            			}
            		});
        		});
        		
        	};
        	
        	$scope.query = function() {
        		if (!$scope.queryData.name) {
        			$scope.errorMsg({message: "查询条件不能为空"});
        			return false;
        		}
        		
        		$timeout(function(){
        			$scope.page = {
                			currentPage: 1
                	};
            		$scope.loadData();
        		});
        	}
        	
        	$scope.loadData = ld;
        	$scope.loadData();
        }
    ]);
});